<template>
  <transition name="toast">
    <div v-if="visible" class="toast" :class="type">
      <div class="toast-icon">
        <span v-if="type === 'success'">✓</span>
        <span v-else-if="type === 'error'">✕</span>
        <span v-else-if="type === 'warning'">⚠</span>
        <span v-else>ℹ</span>
      </div>
      <div class="toast-content">
        <div class="toast-title">{{ title }}</div>
        <div class="toast-message">{{ message }}</div>
      </div>
      <button v-if="type !== 'error'" class="toast-close" @click="close">
        <span>×</span>
      </button>
    </div>
  </transition>
</template>

<script setup>
import { ref, onMounted } from 'vue'

const props = defineProps({
  type: {
    type: String,
    default: 'success',
    validator: (value) => ['success', 'error', 'warning', 'info'].includes(value)
  },
  title: {
    type: String,
    default: ''
  },
  message: {
    type: String,
    default: ''
  },
  duration: {
    type: Number,
    default: 3000
  }
})

const visible = ref(false)
let timer = null

const show = () => {
  visible.value = true
  if (props.duration > 0) {
    timer = setTimeout(() => {
      close()
    }, props.duration)
  }
}

const close = () => {
  visible.value = false
  if (timer) {
    clearTimeout(timer)
    timer = null
  }
}

onMounted(() => {
  show()
})

defineExpose({
  show,
  close
})
</script>

<style scoped>
.toast {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  min-width: 300px;
  max-width: 400px;
  background: #2d2d2d;
  padding: 20px 24px;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  display: flex;
  align-items: center;
  gap: 16px;
  z-index: 9999;
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.toast.success {
  background: #d4edda;
  color: #155724;
  border: 1px solid #c3e6cb;
}

.toast.error {
  color: #dc3545;
  border: 1px solid #f5c6cb;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.toast.warning {
  background: linear-gradient(135deg, #ff9800 0%, #f57c00 100%);
  color: white;
}

.toast.info {
  background: linear-gradient(135deg, #2196f3 0%, #1976d2 100%);
  color: white;
}

.toast-icon {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.25);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: bold;
  flex-shrink: 0;
}

.toast.success .toast-icon {
  background: #28a745;
  color: white;
}

.toast.error .toast-icon {
  background: #dc3545;
  color: white;
}

.toast-content {
  flex: 1;
  min-width: 0;
}

.toast-title {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 6px;
  line-height: 1.3;
}

.toast-message {
  font-size: 15px;
  opacity: 0.9;
  line-height: 1.4;
  word-wrap: break-word;
}

.toast-close {
  background: none;
  border: none;
  color: #6c757d;
  font-size: 20px;
  font-weight: normal;
  cursor: pointer;
  padding: 0;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: all 0.2s ease;
  flex-shrink: 0;
}

.toast-close:hover {
  background: rgba(108, 117, 125, 0.1);
  color: #495057;
}

/* 动画效果 */
.toast-enter-active {
  transition: all 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.toast-leave-active {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.toast-enter-from {
  transform: translate(-50%, -50%) scale(0.8);
  opacity: 0;
}

.toast-leave-to {
  transform: translate(-50%, -50%) scale(0.8);
  opacity: 0;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .toast {
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    margin: 0 20px;
    min-width: auto;
    max-width: none;
    width: calc(100% - 40px);
  }
}
</style>
